package com.zhenyi.youqu.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.utils.AuthUtils;
import com.youqu.common.model.YqUser;

/**
 * 校验用户
 * 
 * @author cen
 *
 */
public class YQUserInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
		String openId = controller.getPara("openId");
		String token = controller.getPara("token");
		if (StrKit.isBlank(openId) && StrKit.isBlank(token)) {
			controller.renderError(401);
			return;
		}
		if (StrKit.notBlank(openId)) {
			// if ("oc2ls5ehaHUgNAyUTTiK_fDVZh9w".equals(openId)) {
			// controller.renderError(401);
			// return;
			// }
			YqUser user = YqUser.dao.searchFirst("openId", openId);
			if (user != null) {
				inv.invoke();
				return;
			}
		}
		if (StrKit.notBlank(token)) {
			String uid = AuthUtils.getTokenUid(controller);
			YqUser user = YqUser.dao.findById(uid);
			if (user != null) {
				inv.invoke();
				return;
			}
		}
		// 如果appId没填,暂时放过
		String appId = controller.getPara("appId");
		if (StrKit.isBlank(appId)) {
			inv.invoke();
			return;
		}
		controller.renderError(401);
		return;
		// if (PropKit.getBoolean("devMode", false)) {
		// user = YqUser.dao.findById(17);
		// }
	}

}
